//
//  SearchDetailViewController.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/1.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class SearchDetailViewController: BaseViewController {
    
    
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var navHeight: NSLayoutConstraint!
    @IBOutlet weak var searchField: UITextField!
    
    var backSearchValue:BackStringValue!
    
    var keyword = ""
    
    var dataArray = [SiteGoodsModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        let flowLayout = UICollectionViewFlowLayout()
        let height = 75.5 + ((UIScreen.ScreenWidth - 30) * 0.5)/(160/130)
        flowLayout.itemSize = CGSize.init(width: (UIScreen.ScreenWidth - 30) * 0.5, height: height)
        flowLayout.scrollDirection = .vertical
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 7, 0, 7)
        flowLayout.minimumLineSpacing = 5
        flowLayout.minimumInteritemSpacing = 16
        
        self.collectionView.setCollectionViewLayout(flowLayout, animated: true)
        self.collectionView.showsVerticalScrollIndicator = false
        
        if Utils.isIphoneX() {
            self.navHeight.constant = 88
        }
        self.searchField.text = self.keyword
        
        Utils.setTextFieldPlaceholderColor(textField: self.searchField)
        self.getSearchDetail()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK:搜索
    @IBAction func searchClick(_ sender: UIButton) {
        guard self.searchField.text != "" else {
            return
        }
        self.keyword = self.searchField.text!
        if self.backSearchValue != nil {
            self.backSearchValue(self.searchField.text!)
        }
        self.searchField.resignFirstResponder()
        self.getSearchDetail()
    }
    
    
    @IBAction func backMethod(_ sender: UIButton) {
        self.backBarButtonMethon()
    }
    
    
    func getSearchDetail(){
        PKhud.show()
        HttpRequest.requestData(.post, URLString: Api.search.rawValue, parameters: ["keyword":self.keyword]) { (result) in
            PKhud.hiden()
            if result["msg"] as! String == "success"{
                if let array = [SiteGoodsModel].deserialize(from: result["data"] as? [Any]) as? [SiteGoodsModel] {
                    self.dataArray = array
                    self.collectionView.reloadData()
                }
            }else{
                PKhud.showText(text: result["data"] as! String)
            }
        }
    }

}

extension SearchDetailViewController:UICollectionViewDelegate,UICollectionViewDataSource{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GoodsCollectionViewCell", for: indexPath) as! GoodsCollectionViewCell
        cell.reload(withModel: self.dataArray[indexPath.row])
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "GoodsDetailViewController") as! GoodsDetailViewController
        ctrl.goodsID = self.dataArray[indexPath.row].id
        self.navigationController?.pushViewController(ctrl, animated: true)
    }
}















